package com.maxmlr.music.common.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.maxmlr.music.common.bean.ResultBean;
import com.maxmlr.music.common.bean.ResultPageBean;
import com.maxmlr.music.common.constant.InterConstant;
import com.maxmlr.music.common.param.PageParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.Optional;


/**
 * @author max
 * @version 1.0
 * @Package com.maxmlr.common.base
 * @ClassName BaseController
 * @Description 控制器基类
 * @Copyright Copyright (c) 2018: www.maxmlr.com.
 * @since 2018年12月15日 下午6:06:33
 */
public abstract class BaseController<M extends BaseService<T>, T> {

    @Autowired
    protected M baseService;

    protected Page getPage(PageParam pageParam) {
        if (Objects.isNull(pageParam)) {
            return new Page<>();
        }
        Integer pageNum = Optional.ofNullable(pageParam.getPageNum()).orElse(InterConstant.pageNum);
        Integer pageSize = Optional.ofNullable(pageParam.getPageSize()).orElse(InterConstant.pageSize);
        Page page = new Page<>(pageNum, pageSize);
        String sort = pageParam.getSort();
        if (StringUtils.isNotBlank(sort)) {
            page.addOrder(OrderItem.asc(sort).setAsc(pageParam.isAsc()));
        }
        return page;
    }

    /**
     * @Description 获取分页列表
     * @author max
     * @since 2019-05-11
     */
    @GetMapping("/page-list")
    @ApiOperation(value = "/page-list", notes = "获取分页列表")
    public ResultPageBean<Object> getPageList(T param, PageParam pageParam) {
        QueryWrapper<T> query = Wrappers.query(baseService.transferInParam(param));
        if (pageParam.isPageFlag()){
            Page<T> page = getPage(pageParam);
            baseService.page(page, query);
            return ResultPageBean.ok(po2PageParams(page));
        } else {
            String sort = pageParam.getSort();
            if (StringUtils.isNotBlank(sort)) {
                if (pageParam.isAsc()) {
                    query.orderByAsc(sort);
                } else {
                    query.orderByDesc(sort);
                }
            }
            List<Object> list = baseService.transferParams(Optional.ofNullable(baseService.list(query))
                    .orElse(Lists.newArrayList()));
            Page<Object> page = getPage(pageParam);
            page.setRecords(list);
            return ResultPageBean.ok(page);
        }

    }

    /**
     * @Description 通过id获取Param
     * @author max
     * @since 2019-05-11
     */
    @GetMapping("/get-one")
    @ApiOperation(value = "/get-one", notes = "通过id获取Param")
    public ResultBean<Object> getParamById(@ApiParam("id") @RequestParam Serializable id) {
        T param = baseService.getById(id);
        return ResultBean.ok(baseService.transferParam(param));
    }


    public Page<Object> po2PageParams(Page<T> page) {
        Page<Object> page1 = new Page<>();
        page1.setRecords(baseService.transferParams(page.getRecords()));
        page1.setTotal(page.getTotal());
        page1.setSize(page.getSize());
        page1.setCurrent(page.getCurrent());
        page1.setOrders(page.getOrders());
        return page1;
    }

}
